package com.tsugun.mediaplayer.view.fragment.subfragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.view.activity.IndexActivity;
import com.tsugun.mediaplayer.view.adapter.FolderItemAdapter;
import com.tsugun.mediaplayer.view.fragment.FolderFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * create an instance of this fragment.
 */
public class FolderListFragment extends SubBaseFragment {

    @BindView(R.id.id_list_recycler)
    RecyclerView mRvFolderList;

    private View mFragment;
    private Context mContext;
    private Unbinder unbinder;
    private IndexActivity indexActivity;

    private List<String> mResult;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mFragment = inflater.inflate(R.layout.fragment_list, container, false);
        mContext = container.getContext();
        unbinder = ButterKnife.bind(this, mFragment);

        initData();
        bindData();
        return mFragment;
    }

    /**
     * 初始化数据
     */
    private void initData() {
        indexActivity = (IndexActivity) mContext;
        mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
    }

    /**
     * 绑定数据
     */
    private void bindData() {
        initFolderList();
    }

    /**
     * 添加结果集合
     *
     * @param result list<MusicModel>
     */
    public void setResult(List<String> result) {
        if (mResult == null) {
            mResult = result;
            return;
        }
        mResult = result;
        initFolderList();
    }

    /**
     * 初始化列表
     */
    private void initFolderList() {
        FolderItemAdapter adapter = new FolderItemAdapter(mContext, mResult);
        adapter.setOnMusicItemClickListener(new FolderItemAdapter.OnFolderItemClickListener() {
            @Override
            public void onItemClick(int position) {
                startFolderFragment(position);
            }
        });
        mRvFolderList.setAdapter(adapter);
        mRvFolderList.setLayoutManager(mLayoutManager);
        mRvFolderList.startLayoutAnimation();

        setSettingTheme(mRvFolderList);
    }

    /**
     * 启动文件夹fragment
     *
     * @param position 文件夹集合中的位置
     */
    private void startFolderFragment(int position) {
        Bundle bundle = new Bundle();
        bundle.putString(FolderFragment.INTENT_STRING_FOLDER_TITLE, mResult.get(position));
        FolderFragment fragment = new FolderFragment();
        fragment.setArguments(bundle);
        indexActivity.startFragment(fragment);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
